package com.email.box.service.impl;

import com.email.box.entity.po.SysUserInfoPO;
import com.email.box.entity.vo.GetSysUserInfoListVO;
import com.email.box.entity.dto.SaveSysUserInfoDTO;
import com.email.box.entity.dto.DeleteCommonDTO;
import com.email.box.entity.converter.SysUserInfoConverter;
import com.email.box.mapper.SysUserInfoMapper;
import com.email.box.service.ISysUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.email.box.common.vo.FilteredParameter;
import com.email.box.common.vo.RespVO;
import com.email.box.common.vo.ApiResultCode;
import com.email.box.utils.FilteredUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户表 服务类
 * </p>
 *
 * @author xuan
 * @since 2021-12-15
 */
@Service
public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUserInfoPO> implements ISysUserInfoService {

    @Override
    public RespVO<List<GetSysUserInfoListVO>> getSysUserInfoList(FilteredParameter dto) {
        QueryWrapper<SysUserInfoPO> queryWrapper = FilteredUtil.getQueryWrapper(dto);
        IPage<SysUserInfoPO> iPage = page(FilteredUtil.getPage(dto), queryWrapper);
        return RespVO.success(SysUserInfoConverter.INSTANCE.getSysUserInfoListVos(iPage.getRecords()), iPage.getTotal());
    }


    @Override
    public RespVO<Object> addSysUserInfo(SaveSysUserInfoDTO dto) {
        SysUserInfoPO sysUserInfoInfo = SysUserInfoConverter.INSTANCE.getSysUserInfoPo(dto);
        boolean add = save(sysUserInfoInfo);
        if (!add) {
            return RespVO.success(RespVO.failure(ApiResultCode.G500, "新增失败"));
        }
        return RespVO.success("新增成功");
    }

    @Override
    public RespVO<Object> updateSysUserInfo(SaveSysUserInfoDTO dto) {
         SysUserInfoPO sysUserInfoInfo = SysUserInfoConverter.INSTANCE.getSysUserInfoPo(dto);
         updateById(sysUserInfoInfo);
         return RespVO.success("保存成功");
    }

    @Override
    public RespVO<Object> saveSysUserInfo(SaveSysUserInfoDTO dto){
        SysUserInfoPO sysUserInfoInfo = SysUserInfoConverter.INSTANCE.getSysUserInfoPo(dto);
        boolean save = saveOrUpdate(sysUserInfoInfo);
        if (!save) {
            return RespVO.success(RespVO.failure(ApiResultCode.G1100, "未修改信息"));
        } else {
            if (Objects.isNull(dto.getAccountId())) {
                return RespVO.success("新增成功");
            } else {
                return RespVO.success("修改成功");
            }
        }
    }

    @Override
    public RespVO<Object> removesSysUserInfo(DeleteCommonDTO dto) {
        List<Long> idList = dto.getIdList();
        boolean remove = removeByIds(idList);
        if (!remove) {
           return RespVO.success(RespVO.failure(ApiResultCode.G500, "删除失败"));
        }
        return RespVO.success("删除成功");
    }
}
